﻿using PeiYue.Common;
using PeiYue.IRepository.Base;
using PeiYue.IServices.Store;
using PeiYue.Model.Dto.Purchase;
using PeiYue.Model.ERPModels;
using PeiYue.Services.BASE;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace PeiYue.Services.Store
{
    public class ReceiptServices : BaseServices<ErpPurchaseOrderDetail>, IReceiptServices
    {
        IBaseRepository<ErpPurchaseOrderDetail> _dal;
        public ReceiptServices(IBaseRepository<ErpPurchaseOrderDetail> dal)
        {
            this._dal = dal;
            base.BaseDal = dal;
        }


        public async Task<List<ErpPurchaseOrderDetail>> GetSpuReceipt(SearchERPPurchaseDetailDTO querymodel)
        {
            StringBuilder sqlWhere = new StringBuilder("");
            sqlWhere.Append(string.IsNullOrEmpty(querymodel.SupplierCode) ? "" : " AND  h.供应商编号=@SupplierCode");
            if (!string.IsNullOrEmpty(querymodel.Supplier))
            {
                querymodel.Supplier = ("%" + querymodel.Supplier + "%");
                sqlWhere.Append(" AND (h.供应商编号 like @Supplier OR h.供应商 like @Supplier)");
            }
            sqlWhere.Append(string.IsNullOrEmpty(querymodel.BarCode) ? "" : " AND  h.供应商编号 in ( select top 1 hh.供应商编号 from [供应链].[dbo].[v采购订单表体]ll left join [供应链].[dbo].[v采购订单表头] hh on ll.[单号]=hh.[单号]  left join [供应链].[dbo].[v仓库拣选日志] mm on mm.采购ID=ll.fID  where mm.条码 =@BarCode )");
            sqlWhere.Append(string.IsNullOrEmpty(querymodel.ScanCode) ? "" : " AND m.条码 =@ScanCode ");
            sqlWhere.Append(querymodel.ScanUser > 0 ? " AND r.UpdateUser =@ScanUser " : "");
            string upDate = DateTime.Now.AddDays(-1).ToShortDateString();

            return await _dal.SqlQuery(@$" select 
 r.Id,l.fID,l.单号, m.数量 as 报货数, ISNULL( l.客户编号,'自拣')客户编号 ,h.要求送货日期 ,h.供应商编号 SupplierCode,h.供应商 SupplierName,
 l.存货编码 , (case when l.品名 like '新品%' then l.品名+' '+ISNULL(销售备注,'') else l.品名 end )as 品名 ,l.[采购单价] ,l.采购计量 ,l.[采购数量] ,l.[金额] 
 , ISNULL( l.[加工要求],'') [加工要求],ISNULL(l.[客户优先级],'') [客户优先级], 账单打印 BillDate ,m.条码 BarCode,r.receiptQty  ,r.UpdateTime ScanDate,r.UpdateUser,u.namec as ReceiptUser
 from [供应链].[dbo].[v采购订单表体] l 
 left join [供应链].[dbo].[v采购订单表头] h on l.[单号]=h.[单号] 
 left join [供应链].[dbo].[v仓库拣选日志] m on   m.采购ID=l.fID
 left join (select * from [OMS].[dbo].[oms_srm_receipt_record] where UpdateTime>{upDate}  and status=1)  r on r.BarCode=m.条码
left join  [OMS].[dbo].[oms_ur_user] u on u.id=r.UpdateUser
 where h.要求送货日期 >= CONVERT(varchar(10),GETDATE(),120)  AND h.类型 in ('正常订单','补货订单')  AND l.[采购数量]>0  AND m.数量>0 AND h.订单状态 in('待报货','待收货' ,'待付款' )  and l.是否附随品!='是'
 {sqlWhere}
 order by  l.[客户优先级] desc,h.供应商", new { querymodel.DeliveryDate, querymodel.SupplierCode, querymodel.CustomerCode, querymodel.SkuCode, querymodel.SaleId, querymodel.Supplier, querymodel.BarCode, querymodel.ScanUser, querymodel.ScanCode });//h.要求送货日期>= CONVERT(varchar(10),GETDATE(),120) h.要求送货日期= '2020-11 -30' and isnull( m.条码,'')!=''AND isnull(r.status,'')!=0  AND isnull(r.status,-999)!=0 OR m.采购ID=l.fID,AND isnull(l.扣库数,0)=0 {(DateTime.Now.Hour >= int.Parse(Appsettings.app(new string[] { "QueryHour", "SupplierHour" })) ? DateTime.Now.AddDays(1).ToShortDateString() : DateTime.Now.ToShortDateString())} 
        }

    }
}
